import Vue from 'vue'
import axios from 'axios'
import { VueJsonp } from 'vue-jsonp'
import utils from 'utils/utils'
import api from 'network/request'
import { Notify } from 'quasar'
import BaiduMap from 'vue-baidu-map'
import state from 'store/state'

Vue.prototype.$axios = axios
Vue.prototype.$utils = utils
Vue.prototype.$api = api
// 挂在Jsonp，百度IP定位使用
Vue.use(VueJsonp)
// 百度地图，使用AK（开发者密钥）
Vue.use(BaiduMap, { ak: state.baiduAK })

// 数据接口API
export function requestApi (config) {
  // 创建实例
  const api = axios.create({
    baseURL: utils.defaultBaseURL,
    // baseURL: 'http://quasar.soonclick.com/api/',
    timeout: 5000,
  })

  api.interceptors.request.use(config => {
    if (localStorage.getItem('Authorization')) {
      config.headers.Authorization = localStorage.getItem('Authorization');
    }
    // console.log(config)
    return config
  }, err => {
    return Promise.reject(err)
  })

  api.interceptors.response.use(result => {
    // console.log(result)
    return result.data
  }, err => {
    // console.log(err.response)
    const defaultNotify = {
      message: '未知错误',
      icon: 'mdi-alert-circle-outline',
      color: 'warning',
      position: 'center',
      timeout: 1500
    }
    switch (err.response.status) {
      case 400:
        defaultNotify.message = '错误请求(400)'
        Notify.create(defaultNotify)
        break
      case 403:
        defaultNotify.message = '拒绝访问(403)'
        Notify.create(defaultNotify)
        break
      case 404:
        defaultNotify.message = '资源不存在(404)'
        Notify.create(defaultNotify)
        break
      case 408:
        defaultNotify.message = '请求超时(408)'
        Notify.create(defaultNotify)
        break
      case 500:
        defaultNotify.message = '服务器错误(500)'
        Notify.create(defaultNotify)
        break
      case 501:
        defaultNotify.message = '服务未实现(501)'
        Notify.create(defaultNotify)
        break
      case 502:
        defaultNotify.message = '网络错误(502)'
        Notify.create(defaultNotify)
        break
      case 503:
        defaultNotify.message = '服务不可用(503)'
        Notify.create(defaultNotify)
        break
      case 504:
        defaultNotify.message = '网络超时(504)'
        Notify.create(defaultNotify)
        break
      case 505:
        defaultNotify.message = 'HTTP版本不受支持(505)'
        Notify.create(defaultNotify)
        break
    }
    if (err.code === 'ECONNABORTED' && err.message.indexOf('timeout') !== -1) {
      defaultNotify.message = '网络异常'
      Notify.create(defaultNotify)
    }
    return Promise.reject(err)
  })

  return api(config)
}

// 百度地图API
export function baiduApi (config) {
  const api = axios.create({
    baseURL: 'http://api.map.baidu.com/',
    timeout: 5000,
  })

  api.interceptors.request.use(config => {
    return config
  }, err => {
    return Promise.reject(err)
  })

  api.interceptors.response.use(result => {
    // console.log(result)
    return result.data
  }, err => {
    // console.log(err.response)
    if (err.code === 'ECONNABORTED' && err.message.indexOf('timeout') !== -1) {
      defaultNotify.message = '网络异常'
      Notify.create(defaultNotify)
    }
    return Promise.reject(err)
  })

  return api(config)
}